<!--
 * @Description:: 请补充填写
 * @Version: 1.0
 * @Author: 小白
 * @Date: 2022-04-13 14:40:58
 * @LastEditors: 小白
 * @LastEditTime: 2025-11-10 11:10:23
 * @FilePath: \tmsadmin\src\views\login\index.vue
 LUCKGIFT
01124083898
-->
<template>
  <div class="login_box">
    <div class="titles"></div>
    <el-card class="form_card">
      <template #header>
        <div class="card-header">
          <img src="@/assets/images/logo.png" class="logo" />
          <span>鲲鹏顺达管理系统</span>
        </div>
      </template>
      <el-form ref="ruleFormRef" :model="loginForm" :rules="rules">
        <el-form-item prop="phone" class="form-item">
          <el-input size="large" placeholder="请输入账号" v-model="loginForm.phone">
            <template #prefix>
              <el-icon>
                <User />
              </el-icon>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input v-model="loginForm.password" type="primary" placeholder="请输入您的密码" style="width: 100%" size="large"
            show-password>
            <template #prefix>
              <el-icon>
                <Lock />
              </el-icon>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item style="width: 100%">
          <el-button type="primary" :loading="loading" size="large" style="width: 100%"
            @click="handleSubmit(ruleFormRef)">
            登录
          </el-button>
        </el-form-item>
        <el-form-item style="width: 100%" class="mb-0"> </el-form-item>
      </el-form>
    </el-card>
  </div>
  <Vcode :show="isShow" @success="onSuccess" @close="onClose" />
</template>
<script lang="ts" setup>

import Vcode from "vue3-puzzle-vcode"
import { ref } from 'vue'
import { ElMessage, FormInstance } from 'element-plus'
import { useRouter } from 'vue-router'

import { aesEncrypt, aesDecrypt } from '@/utils/crypto';
import { setToken } from '../../plugins/cookie'
import { store } from '@/store/index'
import { REQUEST } from '@/api/request'
import { HTTP_URI } from '@/api/base_url'
const router = useRouter()
const ruleFormRef = ref<FormInstance>()
const isShow = ref(false)
const loginForm = ref({
  phone: '',
  password: '',
  avatar: HTTP_URI.IMAGE_URL + '/20250520/1747722044124.jpg'
})
const onClose = () => {
  isShow.value = false
}
const secretKey = ref('01124083898')
const rules = ref({
  phone: [{ required: true, message: '请输入账号', trigger: 'change' }],
  password: [{ required: true, message: '请输入密码', trigger: 'change' }]
})
const IsPhones = () => {
  var info = navigator.userAgent
  var isPhone = /mobile/i.test(info)
  window.localStorage.setItem('isPhone', isPhone)
}
const loading = ref(false)
const onSuccess = () => {
  loading.value = true
  REQUEST.POST('LOGIN', {
    phone: loginForm.value.phone,
    password: loginForm.value.password
  }).then((res: any) => {
    console.log(res, 'login')
    if (res.status !== 200) {
      loading.value = false
      onClose()
    } else {
      if(res.data.isDriver == 1){
         ElMessage({
        type: 'warning',
        message: '当前账号没有权限登录'
      })
       onClose()
      }else{
        setToken(res.data.token)
      onClose()
      const obj = {
        avatar: loginForm.value.avatar ? loginForm.value.avatar : '../../assets/images/tx.png',
        phone: loginForm.value.phone,
        name: res.data.nickName,
        id: res.data.id,
        houseId: res.data.houseId,
        isDriver: res.data.isDriver,
        isLogin:res.data.isLogin,
        password: aesEncrypt(loginForm.value.password, secretKey.value)
      }
      store.commit('setUserInfo', obj)
      ElMessage({
        type: 'success',
        message: '登录成功！'
      })
      IsPhones()
      setTimeout(() => {
        if (res.data.id == 4) {
          router.push({ path: '/news' })
          loading.value = false
        } else {
          router.push({ path: '/' })
          loading.value = false
        }

      }, 2000)
      }
      
    }
  })
}
const handleSubmit = async (formEl: FormInstance | undefined) => {
  if (!formEl) {
    return false
  }
  await formEl.validate((valid: any) => {
    if (valid) {
      isShow.value = true
    }
  })
}
</script>

<style lang="scss" scoped>
.login_box {
  width: 100vw;
  height: 100vh;
  overflow: hidden;
  display: flex;
  justify-content: flex-end;
  align-items: center;
  // background: rgba(0, 7, 39, 1);
  background: url('@/assets/images/login.jpg') no-repeat;
  background-size: 100%;

  //background: linear-gradient(144deg, rgb(7, 119, 247) 19%, rgb(104, 226, 251) 86%);
  .titles {
    font-size: 30px;
    font-weight: bold;
    color: #fff;
    margin-bottom: 20px;
  }

  .form_card {
    width: 400px;
    margin-right: 13vw;

    .card-header {
      text-align: center;
      display: flex;
      justify-content: center;
      align-items: center;
      font-weight: bold;
      font-size: 20px;

      .logo {
        width: 50px;
        margin-right: 10px;
      }
    }
  }
}
</style>
<style lang="scss">
.form_card {
  .el-form-item {
    margin-bottom: 24px !important;
  }

  .mb-0 {
    margin-bottom: 0 !important;
  }

  .el-input__inner {
    padding-left: 0px !important;
  }
}
</style>
